package com.example.proximityview;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener,OnItemClickListener {
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		TabHost tabs = (TabHost)findViewById(R.id.my_tabhost);
	    tabs.setup();
	    //add proximity tab:
	    TabHost.TabSpec proximityTab = tabs.newTabSpec("Proximity");
	    proximityTab.setContent(R.id.Proximity);
	    proximityTab.setIndicator("PROXIMITY");
	    tabs.addTab(proximityTab);
	    //add devices tab:
	    TabHost.TabSpec devicesTab = tabs.newTabSpec("Devices");
	    devicesTab.setContent(R.id.Devices);
	    devicesTab.setIndicator("DEVICES");
	    tabs.addTab(devicesTab);
	    checkBLECompatibility();
	    requestBluetoothBeEnabled();
	    
	    ListView lstPeriphirals = (ListView)findViewById(R.id.lstPeriphirals);
	    lstPeriphirals.setOnItemClickListener(this);
	    ArrayList<String> listItems = new ArrayList<String>();
	    ArrayAdapter<String> lstAdapter = new ArrayAdapter(getBaseContext(),android.R.layout.simple_list_item_1,listItems);
	    lstPeriphirals.setAdapter(lstAdapter);
	    Button btnScan = (Button)findViewById(R.id.btnScan);
	    btnScan.setOnClickListener(this);
	}
	private void checkBLECompatibility(){
//		if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
//			Toast.makeText(this, "FATAL: BLE NOT SUPPORTED ON THIS DEVICE", Toast.LENGTH_SHORT).show();
//			finish();
//		}		
	}
	private void requestBluetoothBeEnabled(){
		

//		// Initializes Bluetooth adapter.
//		final BluetoothManager bluetoothManager =
//		        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//		final BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
//		// Ensures Bluetooth is available on the device and it is enabled. If not,
//		// displays a dialog requesting user permission to enable Bluetooth.
//		if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
//		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//		    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
//		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public void onClick(View arg0) {
		if (arg0 == findViewById(R.id.btnScan)){
			ListView lstPeriphirals = (ListView)findViewById(R.id.lstPeriphirals);
			ArrayAdapter<String> lstAdapter = (ArrayAdapter<String>) lstPeriphirals.getAdapter();
			lstAdapter.add("Clicked "+lstAdapter.getCount());
			lstAdapter.notifyDataSetChanged();
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long id) {
		ListView lstPeriphirals = (ListView)findViewById(R.id.lstPeriphirals);
		ArrayAdapter<String> lstAdapter = (ArrayAdapter<String>) lstPeriphirals.getAdapter();
		TextView lblConnectionStatus = (TextView)findViewById(R.id.lblConnectionStatus);
		lblConnectionStatus.setText("STUB:: Connected to "+lstAdapter.getItem(pos));
		TabHost tabs = (TabHost)findViewById(R.id.my_tabhost);
		tabs.setCurrentTab(0);
	}

}
